﻿// <copyright file="HandleWebBrowser.cs" company="Microsoft">
// Copyright (c) 2010 Microsoft All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-04-23</date>
// <summary>HandleWebBrowser class definition</summary>

namespace Microsoft.WebAnalytics.Behaviors.Handlers
{
    using System;
    using System.ComponentModel.Composition;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.WebAnalytics;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Tracks WebBrowser LoadCompleted and ScriptNotify events
    /// </summary>
    /// <remarks>Silverlight 4</remarks>
    [Tracker(typeof(WebBrowser))]
    public class HandleWebBrowser : ITrackHandler
    {
        #region Properties
        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region ITrackAction Members

        /// <summary>
        /// Track the WebBrowser events
        /// </summary>
        /// <param name="associatedObject">the WebBrowser control</param>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the event arguments</param>
        public void Track(
            System.Windows.FrameworkElement associatedObject,
            AnalyticsEvent analyticsEvent,
            System.EventArgs eventArgs)
        {
            var webBrowser = associatedObject as WebBrowser;

            switch (analyticsEvent.Name)
            {
                case "LoadCompleted":
                    LoadCompleted(analyticsEvent, eventArgs);
                    break;

                case "ScriptNotify":
                    ScriptNotify(analyticsEvent, eventArgs);
                    break;
            }

            this.Log(analyticsEvent);
        }

        /// <summary>
        /// Set the ActionValue in the AnalyticsEvent
        /// </summary>
        /// <param name="analyticsEvent">the Analytics event</param>
        /// <param name="eventArgs">the NotifyEventArgs</param>
        private static void ScriptNotify(AnalyticsEvent analyticsEvent, System.EventArgs eventArgs)
        {
            var args = eventArgs as NotifyEventArgs;
            analyticsEvent.ActionValue = args.Value;
        }

        /// <summary>
        /// Set the AnalyticsEvent.ActionValue and add an AnalyticsEvent.EventArgs value for Content
        /// </summary>
        /// <param name="analyticsEvent">the analytics event</param>
        /// <param name="eventArgs">the NavigationEventArgs</param>
        private static void LoadCompleted(AnalyticsEvent analyticsEvent, System.EventArgs eventArgs)
        {
            var args = eventArgs as NavigationEventArgs;
            analyticsEvent.ActionValue = args.Uri.ToString();
            analyticsEvent.EventArgs.Add(new Data.PropertyValue { PropertyName = "Content", Value = args.Content.ToString() });
        }
        #endregion
    }
}
